package com.fbee.smartcommunities.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fbee.smartcommunities.entity.Permission;
import com.fbee.smartcommunities.mapper.PermissionMapper;
import com.fbee.smartcommunities.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fbee.smartcommunities.utils.CustomizeException;
import com.mysql.cj.util.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 权限菜单表 服务实现类
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-16
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Resource
    private PermissionMapper permissionMapper;

    @Resource(name = "myRedis")
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    @Cacheable(value = "user",key = "'permission'")
    public List<Permission> getlist() {
        return getParentResource(0);
    }

    /**
     * 递归将每个SysResource的子节点都封装进去
     * @param parentId
     * @return
     */
    private List<Permission> getParentResource(Integer parentId){
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        List<Permission> list = permissionMapper.selectList(queryWrapper);
        for (Permission permission : list) {
            // 获得孩子节点，给父节点的属性进行赋值
            List<Permission> parentResource = getParentResource(permission.getId());
            permission.setChildren(parentResource);
        }
        return list;
    }

    @Override
    public Boolean update(Long id, String name, String frontUrl, String backUrl, String description, Integer type) {
        UpdateWrapper<Permission> sysResourceUpdateWrapper = new UpdateWrapper<>();
        sysResourceUpdateWrapper.eq("id",id);
        sysResourceUpdateWrapper.set("name",name);
        sysResourceUpdateWrapper.set("front_url",frontUrl);
        sysResourceUpdateWrapper.set("back_url",backUrl);
        sysResourceUpdateWrapper.set("description",description);
        sysResourceUpdateWrapper.set("type",type);
        return this.update(sysResourceUpdateWrapper);
    }

    @Override
    public Boolean check(String filedName, String value, Long id) {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(filedName,value);
        if(id != null) {
            // 说明这时候是修改时候的校验
            queryWrapper.ne("id",id);
        }
        return count(queryWrapper) > 0;
    }

    @Override
    public Boolean delete(Long id) throws CustomizeException {
        // 判断是否有下级，如何判断 将这id作为父id查询 看是否能够查询出来
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        if(!this.list(queryWrapper).isEmpty()){
            throw new CustomizeException("请先删除下级权限");
        }
        this.removeById(id);
        return true;
    }

    /**
     * 多表查询通过管理员id获取对应权限,再将权限存到缓存中去
     * @param id
     * @return
     */
    @Override
    public List<Permission> getResourcesByAdminId(Long id) {
        Set<String> backUrls = new HashSet<>();
        List<Permission> resources = permissionMapper.getPermissionByUserId(id);
        for (Permission resource : resources) {
            if(!StringUtils.isNullOrEmpty(resource.getBackUrl())){
                String[] backs = resource.getBackUrl().split("\\,");
                for (String back : backs) {
                    backUrls.add(back.trim());
                }
            }
        }
        System.out.println(backUrls);
        // 存到缓存之中
        redisTemplate.opsForValue().set("sys::" + id + "::login",backUrls);
        return resources;
    }

}
